#!/usr/bin/python
# -*- encoding: utf8 -*-

import unittest
import os

import link

class TestLink(unittest.TestCase):
    def setUp(self):
        self.tmp_filename = '__test_link_test.txt'

    def tearDown(self):
        os.remove(self.tmp_filename)

    def testSaveLinkDict(self):
        dicta = link.get_dict()
        link.save_dict(dicta, self.tmp_filename)
        dictb = link.get_dict()
        self.assertDictEqual(dicta, dictb)

        dicta[u'新站'] = 'http://new.org'
        link.save_dict(dicta, self.tmp_filename)
        dictb = link.get_dict(self.tmp_filename)
        self.assertDictEqual(dicta, dictb)

        dicta[u'新站'] = 'http://abc.org'
        link.save_dict(dicta, self.tmp_filename)
        dictb = link.get_dict(self.tmp_filename)
        self.assertDictEqual(dicta, dictb)

    def assertDictEqual(self, dicta, dictb):
        self.assertEqual(len(dicta), len(dictb))

        keys_a = sorted(dicta)
        keys_b = sorted(dictb)
        for keya, keyb in zip(keys_a, keys_b):
            self.assertEqual(keya, keyb)
            self.assertEqual(dicta[keya], dictb[keyb])

if __name__ == '__main__':
    unittest.main()
